// TabBar
const { BaseBehavior } = require('../../mixins/component')
const { TABBAR_ICON, TABBAR_ICON_ACTIVE, TABBAR_REDIRECT } = require('../../constants/tabbar');
const { navTo, reLaunchTo, tipToast } = require('../../common/platform');
const { DEFAULT_THEME } = require('../../config');
const { PAGE_TYPE } = require('../../constants/types');
const { TABBAR_NAV } = require('../../constants/route');

Component({
    behaviors: [BaseBehavior],
    properties: {
        tabList: {
            type: Array,
            value: [],
        },
        activeIndex: {
            type: Number,
            value: 0,
        },
        color: {
            type: String,
            value: '',
        },
        activeColor: {
            type: String,
            value: '',
        },
        locationFail: {
            type: Boolean,
            value: true,
        },
    },
    observers: {
        'activeIndex': function(activeIndex) {
            this.setData({
                selectIndex: activeIndex,
            });
        },
    },
    data: {
        selectIndex: 0,
        icon: TABBAR_ICON,
        iconActive: TABBAR_ICON_ACTIVE,
    },
    methods: {
        selectTabBar: function (e) {
            const dataset = e.currentTarget.dataset;
            const { locationFail } = this.data;
            if (locationFail && dataset.page != PAGE_TYPE.owner && dataset.page != PAGE_TYPE.home) {
                tipToast('请先授权位置信息');
                return;
            }
            this.setData({
                selectIndex: dataset.index,
            });
            if (dataset.page) {
                if (dataset.page === PAGE_TYPE.cart || dataset.page === PAGE_TYPE.classify) {
                    navTo(TABBAR_NAV[dataset.page]);
                } else {
                    reLaunchTo(TABBAR_NAV[dataset.page])
                }
            }
        },
    }
})